//
//  MisListasViewController.m
//  misDeseos
//
//  Created by Roger Plana Casadella on 11/11/11.
//  Copyright (c) 2011 Champions Freak. All rights reserved.
//

#import "MisListasViewController.h"
#import "MainViewController.h"
#import "MisListasTableViewCell.h"
#import "MisListasRegalosTableViewCell.h"
#import <QuartzCore/QuartzCore.h>

@implementation MisListasViewController

@synthesize taula;
@synthesize dpView;
@synthesize dp;
@synthesize mainView;
@synthesize createView;
@synthesize titleTF;
@synthesize endDateTF;
@synthesize categoryTF;
@synthesize cp;
@synthesize cpView;
@synthesize cuentaTF;
@synthesize titlePuntsL;
@synthesize puntsActualsL;
@synthesize costPuntsL;
@synthesize dinersTotalsL;
@synthesize titleRegalos;
@synthesize taulaSub;
@synthesize regalosView;
@synthesize anyadirRegalosView;
@synthesize anyadirTituloTF;
@synthesize anyadirDondeTF;
@synthesize anyadirCatTF;
@synthesize scp;
@synthesize scpView;

@synthesize infoListas = _infoListas;
@synthesize categories = _categories;
@synthesize cuentas = _cuentas;
@synthesize listItems = _listItems;
@synthesize subCategory = _subCategory;
@synthesize selectedList = _selectedList;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
    {
        // Custom initialization
    }
    return self;
}

#pragma mark - Requests

-(void)listsRequest
{
    requestAction = MIS_LISTAS_LISTAS;
    self.infoListas = [[NSMutableArray alloc]init];
    //Posem un notificador d'activitat
    [DSBezelActivityView newActivityViewForView:self.view withLabel:@"Consultando listas...."];
    
    //Hem de fer la petició amb les dades que hi han al formulari
    NSURL *url = [NSURL URLWithString:BASE_URL];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setPostValue:[mvc hashID] forKey:@"h"];
    [request setPostValue:@"lists" forKey:@"c"];
    [request setDelegate:self];
    [request startAsynchronous];
}

-(void)accountsRequest
{
    requestAction = MIS_LISTAS_CUENTAS;
    self.cuentas = [[NSMutableArray alloc]init];
    //Posem un notificador d'activitat
    [DSBezelActivityView newActivityViewForView:self.view withLabel:@"Consultando cuentas...."];
    
    //Hem de fer la petició amb les dades que hi han al formulari
    NSURL *url = [NSURL URLWithString:BASE_URL];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setPostValue:[mvc hashID] forKey:@"h"];
    [request setPostValue:@"accounts" forKey:@"m"];
    [request setDelegate:self];
    [request startAsynchronous];
}

-(void)categoriesRequest
{
    requestAction = MIS_LISTAS_CATEGORIAS;
    self.categories = [[NSMutableArray alloc]init];
    //Posem un notificador d'activitat
    [DSBezelActivityView newActivityViewForView:self.view withLabel:@"Consultando categorias...."];
    
    //Hem de fer la petició amb les dades que hi han al formulari
    NSURL *url = [NSURL URLWithString:BASE_URL];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setPostValue:[mvc hashID] forKey:@"h"];
    [request setPostValue:@"category" forKey:@"m"];
    [request setDelegate:self];
    [request startAsynchronous];
}

-(void)createListRequest
{
    requestAction = MIS_LISTAS_CREAR_LISTA;
    //Posem un notificador d'activitat
    [DSBezelActivityView newActivityViewForView:self.view withLabel:@"Insertando lista...."];
    
    //Composem la string d'arguments
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd"];
    NSString *theDate = [dateFormat stringFromDate:[dp date]];
    [dateFormat release];
    NSString *str = [NSString stringWithFormat:@"%@,%@ 00:00,%@,%@",[titleTF text],theDate,[[self.categories objectAtIndex:selectedCategory] objectAtIndex:0],[[self.cuentas objectAtIndex:selectedAccount] objectAtIndex:0]];
    
    //Hem de fer la petició amb les dades que hi han al formulari
    NSURL *url = [NSURL URLWithString:BASE_URL];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setPostValue:[mvc hashID] forKey:@"h"];
    [request setPostValue:@"lists" forKey:@"c"];
    [request setPostValue:@"create" forKey:@"m"];
    [request setPostValue:str forKey:@"a"];
    [request setDelegate:self];
    [request startAsynchronous];
}

-(void)regalsRequest
{
    self.listItems = [[NSMutableArray alloc]init];
    requestAction = MIS_LISTAS_ITEMS_LISTA;
    //Posem un notificador d'activitat
    [DSBezelActivityView newActivityViewForView:self.view withLabel:@"Consultando regalos...."];
    
    //Hem de fer la petició amb les dades que hi han al formulari
    NSURL *url = [NSURL URLWithString:BASE_URL];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setPostValue:[mvc hashID] forKey:@"h"];
    [request setPostValue:@"items" forKey:@"c"];
    [request setPostValue:[[self.infoListas objectAtIndex:self.selectedList.row]objectAtIndex:MIS_LISTAT_LIST_ID] forKey:@"a"];
    [request setDelegate:self];
    [request startAsynchronous];
}

-(void)subCategoryRequest
{
    requestAction = MIS_LISTAS_SUBCATEGORIAS;
    self.subCategory = [[NSMutableArray alloc]init];
    //Posem un notificador d'activitat
    [DSBezelActivityView newActivityViewForView:self.view withLabel:@"Consultando subcategorias...."];
    
    //Hem de fer la petició amb les dades que hi han al formulari
    NSURL *url = [NSURL URLWithString:BASE_URL];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setPostValue:[mvc hashID] forKey:@"h"];
    [request setPostValue:@"category" forKey:@"m"];
    [request setPostValue:@"subcategory" forKey:@"a"];
    [request setDelegate:self];
    [request startAsynchronous];

}

-(void)addItemRequest
{
    requestAction = MIS_LISTAS_ADD_ITEM;
    //Posem un notificador d'activitat
    [DSBezelActivityView newActivityViewForView:self.view withLabel:@"Insertando regalo...."];
    
    NSString *str = [NSString stringWithFormat:@"%@,%@,%d,%@,%@",[anyadirTituloTF text],[[self.infoListas objectAtIndex:self.selectedList.row]objectAtIndex:MIS_LISTAT_LIST_ID],[self.listItems count],[[self.subCategory objectAtIndex:selectedSubcategory] objectAtIndex:0],[anyadirDondeTF text]];
    //Hem de fer la petició amb les dades que hi han al formulari
    NSURL *url = [NSURL URLWithString:BASE_URL];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setPostValue:[mvc hashID] forKey:@"h"];
    [request setPostValue:@"create" forKey:@"m"];
    [request setPostValue:@"items" forKey:@"c"];
    [request setPostValue:str forKey:@"a"];
    [request setDelegate:self];
    [request startAsynchronous];
}

#pragma mark - Actions

- (IBAction)HomeAction:(id)sender
{
    [mvc LoadDashboard:self];
}
- (IBAction)NewListAction:(id)sender
{
    [titleTF setText:@""];
    [endDateTF setText:@""];
    [categoryTF setText:@""];
    [cuentaTF setText:@""];
    
    //posicionem la vista que volem veure al derrera
    CGRect vistaEntrant = [createView frame];
    vistaEntrant.origin.y = 0;
    

    [UIView animateWithDuration:0.5
                          delay:0.0
                        options: UIViewAnimationCurveEaseInOut
                     animations:^{
                         [createView setFrame:vistaEntrant];
                     } 
                     completion:^(BOOL finished){
                         
                     }];
    
    [self accountsRequest];
}

- (IBAction)CancelAction:(id)sender
{
    [titleTF resignFirstResponder];
    
    //posicionem la vista que volem veure al derrera
    CGRect vistaEntrant = [createView frame];
    vistaEntrant.origin.y = 460;
    
    
    [UIView animateWithDuration:0.5
                          delay:0.0
                        options: UIViewAnimationCurveEaseInOut
                     animations:^{
                         [createView setFrame:vistaEntrant];
                     } 
                     completion:^(BOOL finished){
                         
                     }];
}
- (IBAction)CreateAction:(id)sender
{
    if (titleTF.text.length > 0 && endDateTF.text.length > 0 && categoryTF.text.length > 0 && cuentaTF.text.length > 0)
    {
        [self createListRequest];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                        message:@"Todos los campos son obligatorios"
                                                       delegate:self 
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        
        [alert show];
        [alert release];
    }
}

- (IBAction)DonePickingDate:(id)sender
{
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];
    NSString *theDate = [dateFormat stringFromDate:[dp date]];
    [dateFormat release];
    
    [endDateTF setText:theDate];
    [dpView setHidden:YES];
    
}

- (IBAction)DonePickingCategory:(id)sender;
{
    if (action == 0)
    {
        [categoryTF setText:[[self.categories objectAtIndex:[cp selectedRowInComponent:0]]objectAtIndex:1]];
        selectedCategory = [cp selectedRowInComponent:0];
    }
    else if (action == 1)
    {
        [cuentaTF setText:[[self.cuentas objectAtIndex:[cp selectedRowInComponent:0]]objectAtIndex:1]];
        selectedAccount = [cp selectedRowInComponent:0];
    }
    else
    {
        [anyadirCatTF setText:[[self.subCategory objectAtIndex:[scp selectedRowInComponent:0]]objectAtIndex:1]];
        selectedSubcategory = [scp selectedRowInComponent:0];
    }
    
    [cpView setHidden:YES];
    [scpView setHidden:YES];
}

- (IBAction) backgroundTap:(id) sender
{
    [titleTF resignFirstResponder];
    [anyadirCatTF resignFirstResponder];
    [anyadirDondeTF resignFirstResponder];
    [anyadirTituloTF resignFirstResponder];
}

- (IBAction)EditarRegalosAction:(id)sender
{
    
}
- (IBAction)AddRegaloAction:(id)sender
{
    [anyadirCatTF setText:@""];
    [anyadirDondeTF setText:@""];
    [anyadirTituloTF setText:@""];
    
    //posicionem la vista que volem veure al derrera
    CGRect vistaEntrant = [anyadirRegalosView frame];
    vistaEntrant.origin.y = 0;
    
    
    [UIView animateWithDuration:0.5
                          delay:0.0
                        options: UIViewAnimationCurveEaseInOut
                     animations:^{
                         [anyadirRegalosView setFrame:vistaEntrant];
                     } 
                     completion:^(BOOL finished){
                         
                     }];
    [self subCategoryRequest];
}
- (IBAction)FinalizarListaAction:(id)sender
{
    
}
- (IBAction)BackToListaAction:(id)sender
{
    NSIndexPath *i = [NSIndexPath indexPathForRow:self.selectedList.row inSection:self.selectedList.section];
    self.selectedList = nil;
    [taula reloadData];
    [taula selectRowAtIndexPath:i animated:NO scrollPosition:UITableViewScrollPositionTop];
    
    //posicionem la vista que volem veure al derrera
    CGRect vistaEntrant = [mainView frame];
    vistaEntrant.origin.x = -360;
    [mainView setFrame:vistaEntrant];
    
    CGRect vistaSortint = [regalosView frame];
    vistaEntrant.origin.x = 0;
    vistaSortint.origin.x = 360;
    
    [UIView animateWithDuration:0.5
                          delay:0.0
                        options: UIViewAnimationCurveEaseInOut
                     animations:^{
                         [mainView setFrame:vistaEntrant];
                         [regalosView setFrame:vistaSortint];
                     } 
                     completion:^(BOOL finished){
                         [taula deselectRowAtIndexPath:i animated:YES];
                     }];
}

- (IBAction)BackToListaRegalosAction:(id)sender
{
    [anyadirCatTF resignFirstResponder];
    [anyadirDondeTF resignFirstResponder];
    [anyadirTituloTF resignFirstResponder];
    
    //posicionem la vista que volem veure al derrera
    CGRect vistaEntrant = [anyadirRegalosView frame];
    vistaEntrant.origin.y = 460;
    
    
    [UIView animateWithDuration:0.5
                          delay:0.0
                        options: UIViewAnimationCurveEaseInOut
                     animations:^{
                         [anyadirRegalosView setFrame:vistaEntrant];
                     } 
                     completion:^(BOOL finished){
                         
                     }];
}

- (IBAction)InsertarRegaloAction:(id)sender
{
    if (anyadirTituloTF.text.length > 0 && anyadirDondeTF.text.length > 0 && anyadirCatTF.text.length > 0)
    {
        [self addItemRequest];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                        message:@"Todos los campos son obligatorios"
                                                       delegate:self 
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        
        [alert show];
        [alert release];
    }
}

#pragma mark - UITextField Delegate Methods

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if ( [textField isEqual:endDateTF] ) 
    {
        [titleTF resignFirstResponder];
        [dpView setHidden:NO];
        [cpView setHidden:YES];
        return NO;
    }
    else if ( [textField isEqual:categoryTF] ) 
    {
        action = 0;
        [cp reloadComponent:0];
        [cp selectRow:0 inComponent:0 animated:NO];
        [titleTF resignFirstResponder];
        [dpView setHidden:YES];
        [cpView setHidden:NO];
        return NO;
    }
    else if ( [textField isEqual:cuentaTF] )
    {
        action = 1;
        [cp reloadComponent:0];
        [cp selectRow:0 inComponent:0 animated:NO];
        [titleTF resignFirstResponder];
        [dpView setHidden:YES];
        [cpView setHidden:NO];
        return NO;
    }
    else if ( [textField isEqual:anyadirCatTF] )
    {
        action = 2;
        [scp reloadComponent:0];
        [scp selectRow:0 inComponent:0 animated:NO];
        [scpView setHidden:NO];
        return NO;
    }
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

#pragma mark - ASIHTTPRequest Delegate Methods

-(void)processResponse:(NSString *)response
{
    //Treiem el monitor d'activitat
    [DSBezelActivityView removeViewAnimated:NO];
    
    NSMutableDictionary *loginData = [response JSONValue];
    NSString *responseCode = [loginData objectForKey:@"type"];
    if ([responseCode isEqualToString:@"response"])
    {
        NSString *stat = [loginData objectForKey:@"stat"];
        if ([stat isEqualToString:@"OK"])
        {
            CGRect vistaEntrant;
            NSMutableDictionary *respData;
            switch (requestAction) 
            {
                case MIS_LISTAS_LISTAS:
                    respData = [loginData objectForKey:@"data"];
                    for (NSDictionary *dic in respData)
                    {
                        NSArray *arr = [NSArray arrayWithObjects:[dic objectForKey:@"title"], [dic objectForKey:@"end_date"], [dic objectForKey:@"category"], [dic objectForKey:@"id"],nil];
                        [self.infoListas addObject:arr];
                    }
                break;
                    
                case MIS_LISTAS_CUENTAS:
                    respData = [loginData objectForKey:@"data"];
                    for (NSDictionary *dic in respData)
                    {
                        NSArray *arr = [NSArray arrayWithObjects:[dic objectForKey:@"id"], [dic objectForKey:@"account"], nil];
                        [self.cuentas addObject:arr];
                    }
                    [self categoriesRequest];
                break;
                     
                case MIS_LISTAS_CATEGORIAS:
                    respData = [loginData objectForKey:@"data"];
                    for (NSDictionary *dic in respData)
                    {
                        NSArray *arr = [NSArray arrayWithObjects:[dic objectForKey:@"id"], [dic objectForKey:@"title"], nil];
                        [self.categories addObject:arr];
                    }
                break;
                    
                case MIS_LISTAS_CREAR_LISTA:                    
                    [titleTF resignFirstResponder];
                    
                    //posicionem la vista que volem veure al derrera
                    vistaEntrant = [createView frame];
                    vistaEntrant.origin.y = 460;
                    
                    
                    [UIView animateWithDuration:0.5
                                          delay:0.0
                                        options: UIViewAnimationCurveEaseInOut
                                     animations:^{
                                         [createView setFrame:vistaEntrant];
                                     } 
                                     completion:^(BOOL finished){
                                         [self listsRequest];
                                     }];

                break;
                    
                case MIS_LISTAS_ITEMS_LISTA:
                    respData = [loginData objectForKey:@"data"];
                    for (NSDictionary *dic in respData)
                    {
                        NSArray *arr = [NSArray arrayWithObjects:[dic objectForKey:@"id"], [dic objectForKey:@"title"], [dic objectForKey:@"location"], [dic objectForKey:@"category"], nil];
                        [self.listItems addObject:arr];
                    }
                    [taulaSub reloadData];
                break;
                    
                case MIS_LISTAS_SUBCATEGORIAS:
                    respData = [loginData objectForKey:@"data"];
                    for (NSDictionary *dic in respData)
                    {
                        NSArray *arr = [NSArray arrayWithObjects:[dic objectForKey:@"id"], [dic objectForKey:@"title"], nil];
                        [self.subCategory addObject:arr];
                    }
                break;
                
                case MIS_LISTAS_ADD_ITEM:                    
                    [anyadirDondeTF resignFirstResponder];
                    [anyadirTituloTF resignFirstResponder];
                    
                    //posicionem la vista que volem veure al derrera
                    vistaEntrant = [anyadirRegalosView frame];
                    vistaEntrant.origin.y = 460;
                    
                    
                    [UIView animateWithDuration:0.5
                                          delay:0.0
                                        options: UIViewAnimationCurveEaseInOut
                                     animations:^{
                                         [anyadirRegalosView setFrame:vistaEntrant];
                                     } 
                                     completion:^(BOOL finished){
                                         [self regalsRequest];
                                     }];
                break;    
                
                default:
                break;
            }
        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                            message:@"El nombre de usuario i password no son correctos"
                                                           delegate:self 
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            
            [alert show];
            [alert release];
        }
    }
    else
    {
        NSString *errorStr = [loginData objectForKey:@"msn"];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ERROR"
                                                        message:errorStr
                                                       delegate:self 
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        
        [alert show];
        [alert release];
    }
    
    [taula reloadData];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    // Use when fetching text data
    [self processResponse:[request responseString]];
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
    NSError *error = [request error];
    [DSBezelActivityView removeViewAnimated:YES];
    if ([error code] == 2)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ERROR"
                                                        message:@"The request timed out, try again later"
                                                       delegate:self 
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        
        [alert show];
        [alert release];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ERROR"
                                                        message:@"There has been an error with the server, pleas try again later"
                                                       delegate:self 
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        
        [alert show];
        [alert release]; 
    }
    NSLog(@"ERROR --> %@",[error localizedDescription]);
}

#pragma mark - PickerView Data Sourece Methods

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView 
{    
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component 
{
    if (action == 0) return [self.categories count];
    else if (action == 1)return [self.cuentas count];
    else return [self.subCategory count];
    
}

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{
    if (action == 0) return [[self.categories objectAtIndex:row] objectAtIndex:1];
    else if (action == 1) return [[self.cuentas objectAtIndex:row]objectAtIndex:1];
    else return [[self.subCategory objectAtIndex:row]objectAtIndex:1];
}

#pragma mark - Table View Data Sourece Methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.selectedList == nil) return [self.infoListas count];
    else return [self.listItems count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.selectedList == nil) return 70;
    else return 56;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.selectedList == nil)
    {
        static NSString *CellIdentifier = @"MisListasCellIdentifier";
        MisListasTableViewCell *cell = (MisListasTableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        
        if (cell == nil) 
        {
            NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"MisListasTableViewCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
        }
        
        // Set up the cell...
        NSArray *arr = [self.infoListas objectAtIndex:indexPath.row];
        cell.title.text = [arr objectAtIndex:MIS_LISTAS_LIST_TITLE];
        cell.timeLeft.text = [NSString stringWithFormat:@"Finaliza: %@",[arr objectAtIndex:MIS_LISTAT_LIST_END_DATE]];
        
        switch ([[arr objectAtIndex:MIS_LISTAT_LIST_CAT] intValue]) {
            case 1:
                cell.icon.image = [UIImage imageNamed:@"icon_boda.png"];
            break;
                
            case 4:
                cell.icon.image = [UIImage imageNamed:@"icon_cumple.png"];
            break;
        }
        
        return cell;
    }
    else
    {
        static NSString *CellIdentifier = @"MisListasRegalosCellIdentifier";
        MisListasRegalosTableViewCell *cell = (MisListasRegalosTableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        
        if (cell == nil) 
        {
            NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"MisListasRegalosTableViewCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
        }
        
        // Set up the cell...
        NSArray *arr = [self.listItems objectAtIndex:indexPath.row];
        cell.title.text = [arr objectAtIndex:1];
        cell.encuentralo.text = [arr objectAtIndex:2];
        
        switch ([[arr objectAtIndex:3] intValue]) {
            case 1:
                cell.icon.image = [UIImage imageNamed:@"icon_videojuegos.png"];
            break;
                
            case 2:
                cell.icon.image = [UIImage imageNamed:@"icon_electro.png"];
                break;
        }
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.selectedList == nil)
    {
        self.selectedList = indexPath;
        NSArray *arr = [self.infoListas objectAtIndex:indexPath.row];
        titleRegalos.text = [arr objectAtIndex:MIS_LISTAS_LIST_TITLE];
        
        //posicionem la vista que volem veure al derrera
        CGRect vistaEntrant = [regalosView frame];
        vistaEntrant.origin.x = 360;
        [regalosView setFrame:vistaEntrant];
        
        CGRect vistaSortint = [mainView frame];
        vistaEntrant.origin.x = 0;
        vistaSortint.origin.x = -360;
        
        [UIView animateWithDuration:0.5
                              delay:0.0
                            options: UIViewAnimationCurveEaseInOut
                         animations:^{
                             [regalosView setFrame:vistaEntrant];
                             [mainView setFrame:vistaSortint];
                         } 
                         completion:^(BOOL finished){
                             [self regalsRequest];
                         }];
    }
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    mvc = [MainViewController sharedMainViewController];
    selectedAccount = 0;
    selectedCategory = 0;
    taula.layer.cornerRadius = 5.0f;
    taulaSub.layer.cornerRadius = 5.0f;
    
    titlePuntsL.text = @"Televisor Plasma 22\"";
    puntsActualsL.text = @"14.525";
    costPuntsL.text = @"25.750";
    dinersTotalsL.text = @"350,00 €";

    [self listsRequest];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)dealloc
{
    [super dealloc];
    self.infoListas = nil;
    self.categories = nil;
    self.selectedList = nil;
    self.listItems = nil;
    self.cuentas = nil;
    self.subCategory = nil;
}

@end
